package com.xkdx.guangguang.fragment.brand.photo;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.shareclass.Photo;

public class GridItemAdapter extends BaseAdapter {

	private Context context;
	private List<Photo> list;
	ImageLoader imageLoader;
	DisplayImageOptions options;

	public GridItemAdapter(Context context, List<Photo> list,
			ImageLoader imageLoader) {
		this.context = context;
		this.list = list;
		this.imageLoader = imageLoader;
		this.options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.empty_photo)
				.showImageForEmptyUri(R.drawable.empty_photo).cacheInMemory()
				.cacheOnDisc().build();

	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		if (position >= getCount()) {
			return null;
		}
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		viewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new viewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.brand_gridview_item, null);

			viewHolder.iv = (ImageView) convertView
					.findViewById(R.id.brand_gridview_im);
			viewHolder.tv = (TextView) convertView
					.findViewById(R.id.brand_gridview_commend_number);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (viewHolder) convertView.getTag();
		}

		Photo photo = list.get(position);

		viewHolder.tv.setText(photo.getCommentCount() + "");

		if (photo.getPhotoUrl() != null) {
			// viewHolder.iv.setImageURI(Uri.parse(photo.getPhotoUrl()));
			imageLoader.displayImage(photo.getPhotoUrl(), viewHolder.iv,
					options);
		}

		return convertView;
	}

	public class viewHolder {
		public ImageView iv;
		public TextView tv;
	}

}
